
package com.bear.cmcc.NetTestHTTP.util;

import com.bear.cmcc.NetTestHTTP.component.VillageBean;
import com.bear.cmcc.NetTestHTTP.component.json.respond.CityGridData;

import java.util.Comparator;

@SuppressWarnings("rawtypes")
public class SortCellIntSpeed implements Comparator {
    public final static int signalStrength = 0x0010;

    public final static int electricalLevel = 0x0020;

    public final static int droppingRate = 0x0030;

    public final static int rand = 0x0040;

    public final static int lac = 0x0050;

    public final static int ci = 0x0060;

    public final static int UP = 1;

    public final static int DOWN = -1;

    private int state;

    private int flag;

    /**
     * @param state 排序状态值
     */
    public SortCellIntSpeed(int state, int flag) {
        this.state = state;
        this.flag = flag;
    }

    public int compare(Object obj1, Object obj2) {
        if (obj1 instanceof VillageBean) {
            VillageBean ps1 = (VillageBean) obj1;
            VillageBean ps2 = (VillageBean) obj2;
            if (state == SortCellIntSpeed.DOWN) {
                switch (flag) {
                    case signalStrength:
                        return sortDonw(ps1.signalStrength, ps2.signalStrength);
                    case droppingRate:
                        return sortDonw(ps1.droppingRate, ps2.droppingRate);
                    case lac:
                        return sortDonw(ps1.lac, ps2.lac);
                    case ci:
                        return sortDonw(ps1.ci, ps2.ci);
                }
            }
            switch (flag) {
                case signalStrength:
                    return sortUp(ps1.signalStrength, ps2.signalStrength);
                case lac:
                    return sortUp(ps1.lac, ps2.lac);
                case ci:
                    return sortUp(ps1.ci, ps2.ci);

            }
            return sortUp(ps1.droppingRate, ps2.droppingRate);
        } else if (obj1 instanceof CityGridData) {
            CityGridData ps1 = (CityGridData) obj1;
            CityGridData ps2 = (CityGridData) obj2;

            if (state == SortCellIntSpeed.DOWN) {
                switch (flag) {
                    case signalStrength:
                        return sortDonw(ps1.getSignalStrength(), ps2.getSignalStrength());
                    case droppingRate:
                        return sortDonw(ps1.getFailRate(), ps2.getFailRate());
                }
            }
            switch (flag) {
                case signalStrength:
                    return sortUp(ps1.getSignalStrength(), ps2.getSignalStrength());

            }
            return sortUp(ps1.getFailRate(), ps2.getFailRate());
        }
        return 0;
    }

    /**
     * 升序
     * 
     * @param sp1
     * @param sp2
     * @return
     */
    private int sortUp(int sp1, int sp2) {
        if (sp1 < sp2)
            return -1;
        else if (sp1 > sp2)
            return 1;
        else
            return 0;
    }

    /**
     * 降序
     * 
     * @param sp1
     * @param sp2
     * @return
     */
    private int sortDonw(int sp1, int sp2) {

        if (sp1 > sp2)
            return -1;
        else if (sp1 < sp2)
            return 1;
        else
            return 0;
    }

    /**
     * 升序
     * 
     * @param sp1
     * @param sp2
     * @return
     */
    private int sortUp(String sp1, String sp2) {
        try {
            if (sp1 != null) {
                sp1 = sp1.replace("%", "");
            }
            if (sp1 != null) {
                sp2 = sp2.replace("%", "");
            }

            int d1 = Integer.valueOf(sp1);
            int d2 = Integer.valueOf(sp2);

            if (d1 < d2)
                return -1;
            else if (d1 > d2)
                return 1;
            else
                return 0;
        } catch (NumberFormatException e) {
            e.printStackTrace();
        }
        return 0;
    }

    /**
     * 降序
     * 
     * @param sp1
     * @param sp2
     * @return
     */
    private int sortDonw(String sp1, String sp2) {
        try {
            if (sp1 != null) {
                sp1 = sp1.replace("%", "");
            }
            if (sp1 != null) {
                sp2 = sp2.replace("%", "");
            }

            int d1 = Integer.valueOf(sp1);
            int d2 = Integer.valueOf(sp2);

            if (d1 > d2)
                return -1;
            else if (d1 < d2)
                return 1;
            else
                return 0;
        } catch (NumberFormatException e) {
            e.printStackTrace();
        }
        return 0;
    }

}
